import { reqCategoryList, reqGetBannerList, reqFloorList } from "@/api"
// home模块的小仓库
const actions = {
    // 通过api里面的接口函数调用，向服务器发请求,获取服务器的数据
    async getCategoryList(context) {
        const result = await reqCategoryList()
        // 如果请求成功
        if (result.code === 200) {
            context.commit("GETCATEGORYLIST", result.data)
        }
    },
    /*用then也行 
    categoryList(context) {
        reqCategoryList().then((result) => {
            console.log("categoryList", result)
        })
    }, */
    // 获取轮播图数据
    async getBannerList(context) {
        const result = await reqGetBannerList()
        // 如果请求成功
        if (result.code === 200) {
            context.commit("GETBANNERLIST", result.data)
        }
    },
    // 获取floor数据
    async getFloorList(context) {
        const result = await reqFloorList()
        if (result.code === 200) {
            context.commit("GETFLOORLIST", result.data)
        }
    },
}
const mutations = {
    GETCATEGORYLIST(state, categoryList) {
        state.categoryList = categoryList
    },
    GETBANNERLIST(state, bannerList) {
        state.bannerList = bannerList
    },
    GETFLOORLIST(state, floorList) {
        state.floorList = floorList
    },
}
const state = {
    // state中数据默认初始值，要根据接口的返回值
    categoryList: [],
    // 轮播图的数据
    bannerList: [],
    // floor组件的数据
    floorList: [],
}
// 计算属性
const getters = {}

export default {
    namespaced: true, //开启命名空间
    actions,
    mutations,
    state,
    getters,
}
